// 
// FloatingDockablePanel.cs
//  
// Author:
//       Tomas Vondracek <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Windows.Forms;
using Common.Interfaces;

namespace Common.Gui.GuiWinFormsLib.Controls
{
	internal enum FloatingMode
	{
		/// <summary>
		/// moved to the floating form
		/// </summary>
		InForm,
		/// <summary>
		/// stacked in the panel
		/// </summary>
		InPanel
	}

	public class FloatingDockablePanel : DockablePanel
	{

        /// <summary>
        /// event fired after this panel is dettached to foating form
        /// </summary>
        public event EventHandler DockablePanelBecomeFloating;

        /// <summary>
        /// event fired when the panel is docked back to its container
        /// </summary>
        public event EventHandler DockablePanelReturnFromFloating;
				
		/// <summary>
		/// form used to display content after panel is marked as floating
		/// </summary>
		FloatingForm floatingForm;
		
		/// <summary>
		/// parent of this control in time of creation
		/// </summary>
		Control originParent;
		
		FloatingMode currentMode;
		DockStyle originDockStyle;
        private Button btnFloating;
		
		
        protected ToolStripMenuItem menuFloating;

		public FloatingDockablePanel ()
		{
			this.currentMode = FloatingMode.InPanel;
			
			this.InitializeComponent();
		}
		
		/// <summary>
		/// create FloatingForm and switch to floating mode
		/// </summary>
		protected void Floating()
		{
			this.originParent = this.Parent;
			this.originDockStyle = this.Dock;
			//this.SuspendLayout();
			
			//hide panel from main form
			this.Parent.Controls.Remove(this);
			
			this.floatingForm = new FloatingForm(this);
			this.floatingForm.Text = this.TitleText;
			
			this.floatingForm.Show();
			Logging.Logger.AddInfo("Creating floating form. Panel origin parent is " + this.originParent.ToString());
			Logging.Logger.AddInfo("Creating floating form with size " + this.floatingForm.Size.ToString());
			this.floatingForm.Closed += HandleFloatingFormClosed;
			
			this.currentMode = FloatingMode.InForm;
			this.CurrentState = DockablePanelState.Floating;

            if (this.DockablePanelBecomeFloating != null)
                this.DockablePanelBecomeFloating(this, EventArgs.Empty);
		}
		
		
		protected void HandleFloatingFormClosed (object sender, EventArgs e)
		{
			this.floatingForm.Closed -= HandleFloatingFormClosed;
			
			this.originParent.Controls.Add(this);
			this.Dock = this.originDockStyle;
			this.floatingForm.Dispose();
			
			this.currentMode = FloatingMode.InPanel;
            this.CurrentState = DockablePanelState.Normal;
			this.ShowView();

            if (this.DockablePanelReturnFromFloating != null)
                this.DockablePanelReturnFromFloating(this, EventArgs.Empty);
		}
		
		protected override void OnDockablePanelStateChanged(object sender)
		{
			switch (this.currentState)
			{
			case DockablePanelState.Floating:
            	this.menuFloating.Visible = false;
                this.btnMinimize.Visible = true;
                this.btnRestore.Visible = true;
                this.btnFloating.Visible = false;
                this.menuRestore.Visible = true;
                this.menuHide.Visible = true;
				break;
			default:
                this.menuFloating.Visible = true;
                this.btnFloating.Visible = true;
				
				if (this.currentMode == FloatingMode.InForm)
				{
					this.floatingForm.Close();
                    this.floatingForm.Dispose();
				}
				break;
			}
			
			base.OnDockablePanelStateChanged(sender);
		}
		
		protected override void contextMenuDoackablePanel_ItemClicked (object sender, ToolStripItemClickedEventArgs e)
		{
            // if it is the floating menu item, lets handle it
            // otherwise, send it to the base class
			if (e.ClickedItem.Equals(this.menuFloating))
				Floating();
			else
				base.contextMenuDoackablePanel_ItemClicked (sender, e);	
		}

        private void InitializeComponent()
        {
            this.btnFloating = new System.Windows.Forms.Button();
            this.menuFloating = new System.Windows.Forms.ToolStripMenuItem();
            this.controlPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // controlPanel
            // 
            this.controlPanel.Controls.Add(this.btnFloating);
            this.controlPanel.DoubleClick += new System.EventHandler(this.controlPanel_DoubleClick);
            this.controlPanel.Controls.SetChildIndex(this.btnFloating, 0);
            this.controlPanel.Controls.SetChildIndex(this.lblTitle, 0);
            this.controlPanel.Controls.SetChildIndex(this.btnRestore, 0);
            this.controlPanel.Controls.SetChildIndex(this.btnMinimize, 0);
            // 
            // btnMinimize
            // 
            this.btnMinimize.Location = new System.Drawing.Point(150, 0);
            this.toolTip1.SetToolTip(this.btnMinimize, "Minimize Panel");
            // 
            // btnRestore
            // 
            this.btnRestore.Location = new System.Drawing.Point(166, 0);
            this.toolTip1.SetToolTip(this.btnRestore, "Restore origin size of Panel");
            // 
            // btnFloating
            // 
            this.btnFloating.BackgroundImage = global::Common.Gui.GuiWinFormsLib.Properties.Resources.application_small;
            this.btnFloating.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.btnFloating.Dock = System.Windows.Forms.DockStyle.Right;
            this.btnFloating.Location = new System.Drawing.Point(182, 0);
            this.btnFloating.Name = "btnFloating";
            this.btnFloating.Size = new System.Drawing.Size(16, 14);
            this.btnFloating.TabIndex = 0;
            this.btnFloating.UseVisualStyleBackColor = true;
            this.btnFloating.Click += new System.EventHandler(this.btnFloating_Click);
            // 
            // menuFloating
            // 
            this.menuFloating.Name = "menuFloating";
            this.menuFloating.Size = new System.Drawing.Size(139, 22);
            this.menuFloating.Text = "&Floating";
            this.Controls.SetChildIndex(this.controlPanel, 0);
            this.controlPanel.ResumeLayout(false);
            this.controlPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        void controlPanel_DoubleClick(object sender, EventArgs e)
        {
            if (this.currentMode == FloatingMode.InForm)
            {
                this.contextMenuDoackablePanel_ItemClicked(this, new ToolStripItemClickedEventArgs(this.menuHide)); 
            }
            else
            {
                this.contextMenuDoackablePanel_ItemClicked(this, new ToolStripItemClickedEventArgs(this.menuFloating));
            }
        }

        private void btnFloating_Click(object sender, EventArgs e)
        {
            this.contextMenuDoackablePanel_ItemClicked(this, new ToolStripItemClickedEventArgs(this.menuFloating));
        }

		public void BringFloatingFormToFront()
		{
			if (this.currentMode == FloatingMode.InForm)
			{
				this.floatingForm.BringToFront();
			}
			else 
			{
				Logging.Logger.AddWarning("Trying to bring floating form in front altough it is in panel!");
			}
		}
	}
}
